//Simple Left Sidebar v5.1.1 by DcDeiv https://github.com/dcdeiv
// GPLv2 http://www.gnu.org/licenses/gpl-2.0-standalone.html
(function( $ ) {
	$.fn.simpleLeftSidebar = function( options ) {
		var defaults = {
			settings : {
				data: 'sbplugin',
				opener: undefined,
				ignore: undefined
			},
			leftSidebar: {
				gap: 64,
				closingLinks: 'li',
				style: {
					position: 'fixed',
					top: 0,
					left: 0,
					bottom: 0,
					maxWidth: 350
				}
			}
		},
			config       = $.extend( true, defaults, options ),
			dataName     = config.settings.data,
			$leftSidebar = this,
			$opener      = $( config.settings.opener ),
			ignore       = $( config.settings.ignore )
			w            = $( window ).width(),
			gapL         = config.leftSidebar.gap,
			lsbMaxWidth  = config.leftSidebar.style.maxWidth,
			maxWinWidthL = lsbMaxWidth + gapL,
			lsbw         = w - gapL,
			llinks       = $leftSidebar.find( config.leftSidebar.closingLinks ),
			bodyChildren = $( 'body' )
									.children()
									.not( 'script' )
									.not( $leftSidebar ),
			fix          = $( '*' ).filter(function() {
									return $( this )
										.css( 'position' ) == 'fixed' ; })
										.not( $leftSidebar ),
			cL           = 0;
		
		$leftSidebar.css( config.leftSidebar.style );
		bodyChildren
			.not( ignore )
			.css( 'position', 'relative' );
		
		//Wrapping sb inner content to let it overflow
		$leftSidebar.each(function() {
			$( this ).wrapInner( '<div data-' + dataName + '="subwrapper" />' );
			
			var subWrapper = $( this ).children().filter(function() {
				return $( this ).data( dataName ) === 'subwrapper' ;
			});
			
			subWrapper.css({
				width: '100%',
				height: '100%',
				overflow: 'auto'
			});
		});
		
		//set initial position
		if ( w < maxWinWidthL ) {
			$leftSidebar.css({
				width: lsbw,
				left: -lsbw
			});
		} else {
			$leftSidebar.css({
				width: lsbMaxWidth,
				left: -lsbMaxWidth
			});
		}
		
		$opener.click(function() {
			var lsbWidth     = $leftSidebar.width(),
				countClicksL = function( ee ) {
					return ( ee % 2 === 0 ) ? true : false;
				};
			
			cL++;
			
			if ( countClicksL ( cL ) === false ) {
				bodyChildren
					.add( fix )
					.add( $leftSidebar )
						.animate({
							left: '+=' + lsbWidth
						}, 400);
			} else if ( countClicksL ( cL ) === true ) {
				bodyChildren
					.add( fix )
					.add( $leftSidebar )
						.animate({
							left: '-=' + lsbWidth
						}, 400);
				cL = 0;
			}
		});
		
		llinks.click(function() {
			var lsbWidth = $leftSidebar.width();
			
			bodyChildren
				.add( fix )
				.add( $leftSidebar )
					.animate({
						left: '-=' + lsbWidth
					}, 400);
			
			cL = 0;
		});
		
		$( window ).resize(function() {
			var nsbl,
				w      = $( window ).width(),
				lsbw   = w - gapL,
				sbMarL = parseInt( $leftSidebar.css( 'left' ) );
			
			if ( w < maxWinWidthL ) {
				nsbl = lsbw;
			} else {
				nsbl = lsbMaxWidth;
			}
			
			$leftSidebar.css({
				width: nsbl
			});
			
			if ( sbMarL < 0 ) {
				$leftSidebar.css({
					left: -nsbl
				});
			} else if ( sbMarL === 0) {
				bodyChildren.add( fix ).css({
					left: +nsbl
				});
			}
		});
		
		return this;
	};
}( jQuery ));